from django.db import models

# Create your models here.
from utils.mybase_model import Base


class Goods(Base):  # 商品表
    PAY_TYPE = (
        ('1', '普通'),
        ('2', '促销'),
    )
    course = models.ForeignKey('course.Course', on_delete=models.CASCADE, null=True)
    title = models.CharField('商品名称', max_length=24, default='')
    price = models.DecimalField('商品价格', max_digits=10, null=True, decimal_places=2)
    pay_type = models.CharField('购买渠道', choices=PAY_TYPE, max_length=8, default=1)
    term = models.CharField('有限期限', max_length=30, default=365)
    sales = models.IntegerField('销量', default=0)  # 销量
    is_launched = models.BooleanField("是否上架", default=True)

    class Meta:
        db_table = 'tb_goods'


class Order(Base):
    orderid = models.CharField(max_length=50)
    trade_no = models.CharField('支付宝订单号', max_length=32, null=True)  # 28位
    goods = models.ForeignKey(Goods, on_delete=models.DO_NOTHING, null=True)
    user = models.ForeignKey('user.User', on_delete=models.SET_NULL, null=True)
    sum_price = models.DecimalField('商品总金额', max_digits=10, decimal_places=2, default=999999)
    pay_method = models.CharField(max_length=10, choices=((1, "支付宝"), (2, "微信")), default=1)
    status = models.CharField('支付状态', max_length=10, default=1, choices=((1, "待支付"), (2, "已支付"), (3, "已取消"),))

    # total_amount = models.DecimalField("商品总金额",max_digits=10,decimal_places=2)
    # 优惠卷
    # 收货地址
    class Meta:
        db_table = 'tb_order'


class UserGoods(Base):
    '''
    用户购买的商品
    '''
    user = models.ForeignKey('user.User', on_delete=models.CASCADE)
    goods = models.ForeignKey(Goods, on_delete=models.DO_NOTHING)

    class Meta:
        db_table = "tb_usergoods"
